package cc.idoone.spring_boot_hello.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.idoone.spring_boot_hello.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/thymeleaf")
public class HelloThymeleaf {

	@RequestMapping("/hello/{name}")
	public String hello(@PathVariable("name") String name, Model model, HttpServletRequest req,
			HttpServletResponse resp) {
		// Text
		model.addAttribute("name", name);

		// Bean
		UserBean bean = new UserBean();
		bean.setId("");
		bean.setName("<b>" + name + "</b>");
		model.addAttribute("bean", bean);

		// List
		UserBean bean2 = new UserBean();
		bean2.setId("2");
		bean2.setName("Second");
		UserBean bean3 = new UserBean();
		bean3.setId("3");
		bean3.setName("Third");
		List<UserBean> list = new ArrayList<UserBean>();
		list.add(bean);
		list.add(bean2);
		list.add(bean3);
		model.addAttribute("list", list);

		// UText
		req.setAttribute("hello", "<b>Request</b>: Hello");

		// Map
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", "Alpha");
		map.put("number", 123.456);
		map.put("date", new Date());
		req.setAttribute("map", map);
		return "hello-thymeleaf";
	}
}
